﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using MbUnit.Framework;
using Netkk.Common.Models;
using Netkk.Library.Repositories;
using Netkk.Library.Services;
using Netkk.Library.Validation;
using System;

namespace Netkk.Tests.Links
{
    [TestFixture]
    public class LinksTest
    {
        private ILinkService linkService;

        [SetUp]
        public void Startup()
        {
            ModelStateDictionary modelStateDictionary = new ModelStateDictionary();
            IValidationDictionary validationDictionary = new ModelStateWrapper(modelStateDictionary);
            linkService = new LinkService(validationDictionary, new LinkRepository());
        }

        [TearDown]
        public void Shutdown()
        {
        }

        [Test]
        public void Should_Return_More_Than_Zero_Links()
        {
            IEnumerable<Link> links = linkService.GetAllLinks();

            Assert.GreaterThan(links.Count(), 0);
        }

        [Test]
        [Row("{ebbe7970-8a19-4df4-b36a-4940fdb634d4}")]
        [Row("{0bd0689e-4674-496c-9c30-8b88dce0b0ac}")]
        [Row("{c9dfed82-4417-4a04-8359-628d921f1a7e}")]
        [Row("{99790154-3a9e-440b-bf94-c257bde91dad}")]
        public void Schould_Return_A_Valid_Link_From_A_Given_Id(string guid)
        {
            Link link = linkService.GetLinkById(new Guid(guid));

            Assert.IsNotNull(link);
        }

        [Test]
        [Row("{77777777-8a19-4df4-b36a-4940fdb634d4}")]
        [Row("{0bd0689e-7777-496c-9c30-8b88dce0b0ac}")]
        [Row("{c9dfed82-4417-7777-8359-628d921f1a7e}")]
        [Row("{99790154-3a9e-440b-7777-c257bde91dad}")]
        public void Schould_Not_Return_A_Valid_Link_From_A_Given_Id(string guid)
        {
            Link link = linkService.GetLinkById(new Guid(guid));

            Assert.IsNull(link);
        }

        [Test]
        [Row("Partner")]
        [Row("Mitglieder")]
        [Row("Blogs")]
        public void Should_Return_More_Than_Zero_Links_From_Category(string category)
        {
            IEnumerable<Link> links = linkService.GetAllLinksFromCategory(category);

            Assert.GreaterThan(links.Count(), 0);
        }

        [Test]
        public void Create_Select_And_Delete_A_Link()
        {
            Guid linkId = Guid.NewGuid();
            Link link = new Link
            {
                Id = linkId,
                Name = "TestLinkName",
                Category = "TestLinkCategory",
                Description = "TestLinkDescription",
                Order = 999,
                Url = "TestLinkUrl"
            };

            Assert.IsTrue(linkService.SaveLink(link), "Benutzer wurde nicht erstellt");

            link = linkService.GetLinkById(linkId);

            Assert.IsNotNull(link, "Erstelter Benutzer wurde nicht gefunden");

            linkService.DeleteLink(link);

            link = linkService.GetLinkById(linkId);

            Assert.IsNull(link, "Erstellter Benutzer wurde nicht gelöscht");
        }
    }
}
